<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Level as LevelModel;
use fast\Tree;
use think\Db;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
 * 分类管理
 *
 * @icon   fa fa-list
 * @remark 用于统一管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
 */
class Projectlist extends Backend
{

    /**
     * @var \app\common\model\Category
     */
    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\common\model\Projectlist');
        $typeList = LevelModel::getTypeList();
        $this->assignconfig('typeList', $typeList);
        $this->view->assign("statusList", $this->model->getStatusList());

    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $filter = (array)$this->request->request('filter');
            list($where, $sort, $order, $offset, $limit,$paytype) = $this->buildparams();
            //最新
            if($paytype == "1"){
                $where1 = "type = 1";
            }else{
                $where1 = "";
            }


            $total = $this->model
                ->where($where)
                ->where($where1)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($where1)
                ->limit($offset, $limit)
                ->select();


            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            return json($result);

        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $plist =db("project_list");

        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if(!empty($params["imgs"])){
                    $imgs = explode(",",$params["imgs"]);
                    $params["img"] = $imgs[0];
                }else{
                    $params["img"] = "";
                }

                if(!empty($params["vedios"])){
                    $vedios = explode(",",$params["vedios"]);
                    $params["vedio"] = $vedios[0];
                }else{
                    $params["vedio"] = "";
                }


                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }

                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        Db::commit();
                        $this->success();
                    } else {
                        Db::rollback();
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        //0正常 1 超前 2 滞后 3完成
        if($row['status'] == 0){
            $row['statustext'] = '正常';
        }elseif ($row['status'] == 1){
            $row['statustext'] = '超前';
        }elseif ($row['status'] == 2){
            $row['statustext'] = '滞后';
        }elseif ($row['status'] == 3){
            $row['statustext'] = '完成';
        }else{
            $row['statustext'] = '未知';
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    //数据校验 单个汇报
    public function check(){

        if (empty($this->request->post('file'))) {
            $this->error('请上传文件');
        }
        vendor('phpexcel.PHPExcel');
        $filePath = $this->request->post('file');

        $arr = explode('.', $filePath);
        if ($arr[count($arr) - 1] != 'xlsx' && $arr[count($arr) - 1] != 'xls') {
            $this->error('文件格式不正确');
        }

        //清空历史校验数据
        db("projectlisterror")->execute("TRUNCATE fa_projectlisterror");


        $objreder = \PHPExcel_IOFactory::load(ROOT_PATH . DS . 'public' . DS . $filePath);
        $sheet = $objreder->getActiveSheet();
        $merge = $sheet->getMergeCells();//获取合并的单元格数据
        $varchar = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
            "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ"];
        //取列合并数据
        $mergeColumn = [];//列合并
        $mergeRow = [];//行合并
        if ($merge) {
            foreach ($merge as $cells) {
                $str = explode(':', $cells);
                if (count($str) >= 2) {
                    if (substr($str[0], 0, 1) == substr($str[1], 0, 1)) {//代表列合并
                        $mergeColumn[] = $cells;
                    } else {
                        $mergeRow[] = $cells;
                    }
                }
            }
        }
        $sheetCount = $objreder->getSheetCount();//获取excel文件里有多少个sheet

        //这里写死只读sheet1
        for ($i = 0; $i < 1; $i++) {
            $data = $objreder->getSheet($i)->toArray();//读取每个sheet里的数据 全部放入到数组中
        }
        //暂时校验 项目名称（用来匹配项目）
        $errordata = [];
        $sucdata = [];
        $now = time();
        $len = count($data);
        $prodb = db('project');
        $prohbdb = db('projectlist');//单个汇报表
        $nowyear = date("Y",$now);

        //判断否校验图片 如果校验 则判断excel 路径下是否有图片
        $upimg  = false;
        $config = db("config")->where(['id'=>24])->field("value")->find();
        if($config && $config['value'] == "是"){
            $upimg = true;
        }

        //这里采取固定盘符下去图片和视频的资源  固定 D:resource 文件夹

        //判断否校验视频 如果校验 则判断excel 路径下是否有视频
        $upvideo  = false;
        $config = db("config")->where(['id'=>25])->field("value")->find();
        if($config && $config['value'] == "是"){
            $upvideo = true;
        }
        //图片视频资源路径
        $files =  array();
        $path = 'D:/resource';

        for ($j=7;$j<$len;$j++){

            if (is_string($data[$j][0]) && strlen(trim($data[$j][0])) >= 6) {//粗略判断是否为填报单位
                continue;
            } else {
                //校验 项目名称
                $remark = "";
                $error = [];
                $title = preg_replace('# #', '', $data[$j][1]);
                if(empty($title)){
                    $error['number'] = "项目编号：".$data[$j][0];
                    $error['title'] = "";
                    $remark .= "具体项目名称：不能为空|";
                    $error['ctime'] = $now;
                }else{
                    //判断 名称是否重复
                    if(in_array($title,$sucdata)){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "具体项目名称：不能重复|";
                        $error['ctime'] = $now;
                    }else{
                        array_push($sucdata,$title);
                    }

                }

                //判断项目是否存在
                if($title){
                   $tres = $prodb->where(['title'=>$title])->find();
                   if(!$tres){
                       $error['number'] = "项目编号：".$data[$j][0];
                       $error['title'] = $title;
                       $remark .= "严重错误：没有找到基础数据，请核实具体项目名称是否被修改|";
                       $error['ctime'] = $now;
                   }
                }

                // 校验项目（计划）完成时间
                $proenddate = preg_replace('# #', '', $data[$j][11]);
                if($proenddate){
                    //校验日期格式 必须是年和月
                    $cyear =  substr_count($proenddate,'年');
                    if($cyear != 1){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "项目(计划)完成时间：时间不合法、必须包含年|";
                        $error['ctime'] = $now;
                    }
                    $cmonth =  substr_count($proenddate,'月');
                    if($cmonth != 1){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "项目(计划)完成时间：时间不合法、必须包含月|";
                        $error['ctime'] = $now;
                    }

                    $proenddatetmp = str_replace("年","-",$proenddate);
                    $proenddatetmp = str_replace("月","-",$proenddatetmp);
                    $proenddatetmp = rtrim($proenddatetmp,"-");
                    $proenddate = $proenddatetmp;
                    $proenddateint  = strtotime($proenddate);
                    $oldyear = date("Y",$proenddateint);
                    if($oldyear < $nowyear){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "项目(计划)完成时间：不能小于今年|";
                        $error['ctime'] = $now;
                    }

                }

                //校验 阶段总评估 不能为空
                $assessment = preg_replace('# #', '', $data[$j][14]);
                if(empty($assessment)){
                    $error['number'] = "项目编号：".$data[$j][0];
                    $error['title'] = $title;
                    $remark .= "阶段总评估：不能为空|";
                    $error['ctime'] = $now;
                }

                //校验 年度完成工作量自评
                $prooldpecent = preg_replace('# #', '', $data[$j][15]);
                if(empty($prooldpecent)){
                    $error['number'] = "项目编号：".$data[$j][0];
                    $error['title'] = $title;
                    $remark .= "年度完成工作量自评：不能为空|";
                    $error['ctime'] = $now;
                }else{
                    $pres = check_str($prooldpecent,'%');
                    if($pres != 1){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "年度完成工作量自评：数据不合法，请输入正确的百分数|";
                        $error['ctime'] = $now;
                    }
                    //简单判断 如果去掉百分比 是否大于100
                    $percent = str_replace("%","",$prooldpecent);
                    if($percent > 100){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "年度完成工作量自评：数据不合法，百分数不能超过100|";
                        $error['ctime'] = $now;
                    }

                }

                // 校验 汇报日期 这里考虑 如果用户3,4月没有汇报，5，6月一次性汇报 所以这里这个汇报时间是关键

                $hbdate = preg_replace('# #', '', $data[$j][21]);
                $hbdate2 = preg_replace('# #', '', $data[$j][21]);
                if(empty($hbdate)){
                    $error['number'] = "项目编号：".$data[$j][0];
                    $error['title'] = $title;
                    $remark .= "汇报日期：不能为空|";
                    $error['ctime'] = $now;
                }
                if($hbdate){

                    //汇报日期必须是 2021/12/21 这种格式
                    $rule =  substr_count($hbdate,'/');
                    if($rule != 2){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "汇报日期：时间不合法、必须包固定'/'格式|";
                        $error['ctime'] = $now;
                    }

                    $hbdateint  = strtotime($hbdate);

                    $oldyear = date("Y",$hbdateint);
                    $oldmonth = date("m",$hbdateint);

                    $oldmonthint = (int)$oldmonth;
                    //只取出双月
                    if($oldmonthint%2 == 1){
                        $oldmonth = $oldmonthint+1;
                        if($oldmonth <=9){
                            $oldmonth = "0".$oldmonth;
                        }

                    }

                    $hb_timedate = $oldyear."-".$oldmonth; //用于查询

                    //获取某月的最后一天
                    $ymday = date('Y-m-t',strtotime($hb_timedate));
                    $hb_timedate = $ymday;
                    if($oldyear != $nowyear){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "汇报日期：必须是今年|";
                        $error['ctime'] = $now;
                    }

                }

                //最后校验当次是否提交了多次 提交的数据 都按照双月时间存储 比如 2021-03月汇报的，则会记录为 2021-04 所以  只有查询对应月是否有记录
                $hbres = $prohbdb->where(['pid'=>$tres['id'],'hbcdate'=>$hb_timedate])->field("id,hbdate,title")->find();
                if($hbres){
                    $error['number'] = "项目编号：".$data[$j][0];
                    $error['title'] = $title;
                    $remark .= "汇报日期：该项目在".$hbres['hbdate']."进行了汇报，如果需要重新汇报，请删除上次汇报,";
                    $error['ctime'] = $now;
                }

                //如果开启校验图片和视频
                if($upimg && $upvideo){
                    $name = $tres['title'];
                    //校验是否有图片 多个项目逐个校验
                    $resource = get_allfiles($path,$files);
                    if(!array_key_exists($name,$resource)){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "图片/视频：未找到汇报的图片和视频";
                        $error['ctime'] = $now;
                    }
                    //判断图片是否存在
                    if(array_key_exists($name,$resource)){
                        if(!array_key_exists("jpg",$resource[$name])){
                            $error['number'] = "项目编号：".$data[$j][0];
                            $error['title'] = $title;
                            $remark .= "图片：未找到汇报的图片";
                            $error['ctime'] = $now;
                        }
                    }

                    //判断视频是否存在
                    if(array_key_exists($name,$resource)){
                        if(!array_key_exists("mp4",$resource[$name])){
                            $error['number'] = "项目编号：".$data[$j][0];
                            $error['title'] = $title;
                            $remark .= "视频：未找到汇报的视频";
                            $error['ctime'] = $now;
                        }
                    }

                }

                //如果只开启校验图片
                if($upimg && !$upvideo){
                    $name = $tres['title'];
                    //校验是否有图片 多个项目逐个校验
                    $resource = get_allfiles($path,$files);
                    if(!array_key_exists($name,$resource)){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "图片：未找到汇报的图片";
                        $error['ctime'] = $now;
                    }
                    //判断图片是否存在
                    if(array_key_exists($name,$resource)){
                        if(!array_key_exists("jpg",$resource[$name])){
                            $error['number'] = "项目编号：".$data[$j][0];
                            $error['title'] = $title;
                            $remark .= "图片：未找到汇报的图片";
                            $error['ctime'] = $now;
                        }
                    }

                }

                //如果只开启校验视频
                if(!$upimg && $upvideo){
                    $name = $tres['title'];
                    //校验是否有图片 多个项目逐个校验
                    $resource = get_allfiles($path,$files);
                    if(!array_key_exists($name,$resource)){
                        $error['number'] = "项目编号：".$data[$j][0];
                        $error['title'] = $title;
                        $remark .= "视频：未找到汇报的视频";
                        $error['ctime'] = $now;
                    }

                    //判断视频是否存在
                    if(array_key_exists($name,$resource)){
                        if(!array_key_exists("mp4",$resource[$name])){
                            $error['number'] = "项目编号：".$data[$j][0];
                            $error['title'] = $title;
                            $remark .= "视频：未找到汇报的视频";
                            $error['ctime'] = $now;
                        }
                    }

                }



                if($remark){
                   // $error['remark'] = substr($remark,0,strlen($remark)-1);
                    $error['remark'] = $remark;
                }

            }
            if(count($error) > 0){
                $errordata[] = $error;
            }

        }

        if($errordata){
            $isid = db("projectlisterror")->insertAll($errordata);
            $this->error("单个汇报数据有误，请核实修改后重新校验");
        }else{
            $this->success("校验成功，请进行数据导入");
        }


    }

    //汇报数据导入
    public function import()
    {

        ini_set('max_execution_time','0');

        if (empty($this->request->post('file'))) {
            $this->error('请上传文件');
        }
        vendor('phpexcel.PHPExcel');
        $filePath = $this->request->post('file');

        $arr = explode('.', $filePath);
        if ($arr[count($arr) - 1] != 'xlsx' && $arr[count($arr) - 1] != 'xls') {
            $this->error('文件格式不正确');
        }
        $objreder = \PHPExcel_IOFactory::load(ROOT_PATH . DS . 'public' . DS . $filePath);
        $sheet = $objreder->getActiveSheet();
        $merge = $sheet->getMergeCells();//获取合并的单元格数据
        $varchar = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
            "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ"];
        //取列合并数据
        $mergeColumn = [];//列合并
        $mergeRow = [];//行合并
        if ($merge) {
            foreach ($merge as $cells) {
                $str = explode(':', $cells);
                if (count($str) >= 2) {
                    if (substr($str[0], 0, 1) == substr($str[1], 0, 1)) {//代表列合并
                        $mergeColumn[] = $cells;
                    } else {
                        $mergeRow[] = $cells;
                    }
                }
            }
        }
        $sheetCount = $objreder->getSheetCount();//获取excel文件里有多少个sheet

        //清空历史校验数据
        db("projectlisterror")->execute("TRUNCATE fa_projecterror");

        //这里写死只读sheet1
        for ($i = 0; $i < 1; $i++) {
            $data = $objreder->getSheet($i)->toArray();//读取每个sheet里的数据 全部放入到数组中
        }


        $errordata = [];
        $now = time();
        $remark = '';
        $sucdata = [];
        $len = count($data);
        $prodb = db('project');
        $prohbdb = db('projectlist');//单个汇报表
        $nowyear = date("Y",$now);
        //这里采取固定盘符下去图片和视频的资源  固定 D:resource 文件夹

        //判断否校验图片 如果校验 则判断excel 路径下是否有图片
        //图片视频资源路径
        $files =  array();
        $path = 'D:/resource';

        $upimg  = false;
        $config = db("config")->where(['id'=>24])->field("value")->find();
        if($config && $config['value'] == "是"){
            $upimg = true;
        }
        $upvideo  = false;
        $config = db("config")->where(['id'=>25])->field("value")->find();
        if($config && $config['value'] == "是"){
            $upvideo = true;
        }


        //外层事务 要么都成功，要么都失败，单条成功也不会的导入。
        Db::startTrans();
        try {

            for ($j = 7; $j < count($data); $j++) {

                    //项目名称
                    $title = preg_replace('# #', '', $data[$j][1]);
                    $details = Db::table('fa_project')->where('title', $title)->where('title', 'neq', '')->field('id,prostartdate,proenddate')->find();

                    if(!$details){
                        continue;//跳过 1-7 分类
                    }else{
                        $isDetails = true;
                    }

                    //更新主表记录
                    $project = [];
                    $project['utime'] = $now;

                    //单个汇报表
                    $detail['pid'] = $details['id'];
                    $detail['title'] = $title;
                    $detail['excel'] = $filePath;
                    $detail['type'] = '1';
                    $detail['remark'] = preg_replace('# #', '', $data[$j][22]); //备注
                    $detail['createtime'] = $now;
                    //用户录入的汇报日期
                    $hbdate = preg_replace('# #', '', $data[$j][21]);

                    if($hbdate){
                        $detail['hbdate'] = $hbdate; //原始汇报日期
                      /*  $hbdatetmp = str_replace("年","-",$hbdate);
                        $hbdatetmp = str_replace("月","-",$hbdatetmp);
                        $hbdatetmp = rtrim($hbdatetmp,"-");
                        $hbdate = $hbdatetmp;*/
                        $hbdateint  = strtotime($hbdate);
                        $oldyear = date("Y",$hbdateint);
                        $oldmonth = date("m",$hbdateint);
                        $oldmonthint = (int)$oldmonth;
                        //只取出双月
                        if($oldmonthint%2 == 1){
                            $oldmonthint = $oldmonthint+1;
                            if($oldmonthint <=9){
                                $oldmonth = "0".$oldmonthint;
                            }
                        }else{
                            if($oldmonthint <=9){
                                $oldmonth = "0".$oldmonthint;
                            }
                        }
                        $hb_timedate = $oldyear."-".$oldmonth; //用户查询 固定20号 方便搜索
                        $ymday = date('Y-m-t',strtotime($hb_timedate));
                        $detail['hbcdate'] = $ymday; //转换后的汇报日期
                    }


                    $detail['assessment'] = preg_replace('# #', '', $data[$j][14]);//阶段总评
                    $userpercent = $detail['userpercent'] = preg_replace('# #', '', $data[$j][15]);//年度完成自评百分比 用户提交

                    //程序计算的遍历
                    // 校验项目（计划）完成时间 如果存在 则取excel 否则 从主表取
                    $proenddate = preg_replace('# #', '', $data[$j][11]);
                    if(!$proenddate){
                        $proenddate = $details['proenddate'];
                        $detail['proedate'] = $details['proenddate'];
                        $edate = $proenddate;
                    }else{
                        $proenddatetmp = str_replace("年","-",$proenddate);
                        $proenddatetmp = str_replace("月","-",$proenddatetmp);
                        $proenddatetmp = rtrim($proenddatetmp,"-");
                        $proenddate = $proenddatetmp;
                        $project['proenddate'] = $proenddate;
                        $detail['proedate'] = $proenddate;
                        $edate = $proenddate;
                    }
                    //项目开始时间
                    $prostartdate = preg_replace('# #', '', $data[$j][12]);
                    if(!$prostartdate){
                        $prostartdate = $details['prostartdate'];
                        $detail['prosdate'] = $details['prostartdate'];
                        $sdate = $prostartdate;
                    }else{
                        $prostartdatetmp = str_replace("年","-",$prostartdate);
                        $prostartdatetmp = str_replace("月","-",$prostartdatetmp);
                        $prostartdatetmp = rtrim($prostartdatetmp,"-");
                        $prostartdate = $prostartdatetmp;
                        $project['prostartdate'] = $prostartdate;
                        $detail['prosdate'] = $prostartdate;
                        $sdate = $prostartdate;

                    }

                    $comRes = comhbdate($sdate,$edate);
                    if($comRes){
                        $detail['percent'] = $comRes['percent'] * 100 ."%";
                        $itempecent =  $comRes['itempecent'];
                        $detail['itempecent'] = $itempecent * 100 ."%";
                        $detail['psdate'] = $comRes['sdate'];
                        $detail['pedate'] = $comRes['edate'];
                    }else{
                        Db::rollback();
                        $this->error("数据导入失败");
                    }

                    //判断项目状态 0正常 1 超前 2 滞后 3 完成 这里做一个区间考虑 由于 百分比 四舍五入的情况 所以考虑一点误差
                    $userpercent = str_replace("%","",$userpercent);
                    $userpercent =  $userpercent/100;
                    $userpercent = substr(sprintf("%.3f",$userpercent),0,-1);

                    //完成的标识 除了之后 其他状态只要是最后一次汇报 则代表完成 如果是本年度 但也算完成，这里的完成指 总项目的完成 而非本年度完成

                    $proenddate2 = $proenddate;
                    $lastyear = date("Y",strtotime($proenddate2));
                    $lastmonth = date("m",strtotime($proenddate2));
                    $lastmonth = (int)$lastmonth;
                    //只取出双月
                    if($lastmonth%2 == 1){
                        $lastmonth = $lastmonth+1;
                        if($lastmonth <=9){
                            $lastmonth = "0".$lastmonth;
                        }
                    }else{
                        if($lastmonth <=9){
                            $lastmonth = "0".$lastmonth;
                        }
                    }
                   $last_timedate = $lastyear."-".$lastmonth; //用户查询

                    if($userpercent < $itempecent){
                        $project['status'] = 2;
                        $detail['status'] = 2;
                    }elseif ($userpercent > $itempecent){
                        $project['status'] = 1;
                        $detail['status'] = 1;
                        if($hb_timedate == $last_timedate){
                            $project['status'] = 3;
                            $detail['status'] = 3;
                        }
                    }else{
                        $project['status'] = 0;
                        $detail['status'] = 0;
                        if($hb_timedate == $last_timedate){
                            $project['status'] = 3;
                            $detail['status'] = 3;
                        }
                    }

                    //更新和新增数据
                    $proeff = $prodb->where(['id'=>$details['id']])->update($project);
                    //将历史数据更新为old
                    $hbup['updatetime'] = $now;
                    $hbup['type'] = '0';
                    $hbeff = $prohbdb->where(['pid'=>$details['id']])->update($hbup);
                    //新增
                    $hbid = $prohbdb->insertGetId($detail);

            }

            Db::commit();

        } catch (\Exception $e) {
            $this->error($e->getMessage());
            Db::rollback();
        }

        if($errordata){
            Db::rollback();
            $this->error("单个汇报数据有误，请核实修改后重新校验");
        }else{
            $this->success("数据汇报成功");
        }

    }


    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }
}
